package com.slwish.xty.mvp.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.slwish.common.mvp.presenter.BasePresenter;
import com.slwish.common.utils.LogUtils;
import com.slwish.xty.R;
import com.slwish.xty.config.UrlConfig;
import com.slwish.xty.mvp.contract.SignContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.ProgressDialogCallBack;
import com.zhouyou.http.exception.ApiException;

/**
 * 模块说明：签到
 *
 * @author 陈浩
 * @date 2017/12/6
 */

public class SignPresenter extends BasePresenter<SignContract.View> implements SignContract.Presenter {
    public SignPresenter(SignContract.View view) {
        super(view);
    }

    @Override
    public void signInSubscribe(Context mContext, int orderId, double longitude, double latitude) {
        EasyHttp.post(UrlConfig.SIGNIN_SUBSCRIBE_URL)
                .params("orderId", orderId + "")
                .params("longitude", longitude + "")
                .params("latitude", latitude + "")
                .execute(new ProgressDialogCallBack<String>(showProgressDialog(mContext)) {
                    @Override
                    public void onSuccess(String json) {
                        LogUtils.i("xx", "签到---" + json);
                        try {
                            JSONObject jsonObject = JSON.parseObject(json);
                            String resultCode = (String) jsonObject.get("resultCode");
                            String resultMessage = (String) jsonObject.get("resultMessage");
                            //return
                            if (!TextUtils.equals(resultCode, "0")) {
                                setError(resultMessage);
                                return;
                            }
                            if (getView() != null) {
                                setError(R.string.sign_success_text);
                                getView().signInSubscribeSuccess();
                            }
                        } catch (Exception e) {
                            setError(R.string.sign_failed_text);
                        }
                    }

                    @Override
                    public void onError(ApiException e) {
                        super.onError(e);
                        setError(R.string.sign_failed_text);
                    }
                });

    }
}
